<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>大数据之多范式编程语言 Scala07_集合 | Oct25-X</title><meta name="keywords" content="Scala"><meta name="author" content="Oct25-X"><meta name="copyright" content="Oct25-X"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="集合继承图 Scala的集合有三大类：序列 Seq、集合 Set、映射 Map，所有的集合都扩展自 Iterable 特质   于几乎所有的集合类，Scala 都同时提供了可变和不可变的版本，分别位于以下两个包  不可变集合：scala.collection.immutable 可变集合：scala.collection.mutable   Scala 不可变集合，就是指该集合对象不可修改，每次修">
<meta property="og:type" content="article">
<meta property="og:title" content="大数据之多范式编程语言 Scala07_集合">
<meta property="og:url" content="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/index.html">
<meta property="og:site_name" content="Oct25-X">
<meta property="og:description" content="集合继承图 Scala的集合有三大类：序列 Seq、集合 Set、映射 Map，所有的集合都扩展自 Iterable 特质   于几乎所有的集合类，Scala 都同时提供了可变和不可变的版本，分别位于以下两个包  不可变集合：scala.collection.immutable 可变集合：scala.collection.mutable   Scala 不可变集合，就是指该集合对象不可修改，每次修">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt50.png">
<meta property="article:published_time" content="2024-02-22T08:16:10.000Z">
<meta property="article:modified_time" content="2024-02-24T11:06:25.015Z">
<meta property="article:author" content="Oct25-X">
<meta property="article:tag" content="Scala">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt50.png"><link rel="shortcut icon" href="/oct25-xxxxx/img/logo.jpg"><link rel="canonical" href="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/oct25-xxxxx/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/oct25-xxxxx/',
  algolia: undefined,
  localSearch: {"path":"search.json","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '大数据之多范式编程语言 Scala07_集合',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-24 19:06:25'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/rightMenu.css"><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg24.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/oct25-xxxxx/">Oct25-X</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">大数据之多范式编程语言 Scala07_集合</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-24T11:06:25.015Z" title="更新于 2024-02-24 19:06:25">2024-02-24</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/oct25-xxxxx/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="大数据之多范式编程语言 Scala07_集合"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="集合继承图"><a href="#集合继承图" class="headerlink" title="集合继承图"></a>集合继承图</h1><ol>
<li><p>Scala的集合有三大类：<strong>序列 Seq、集合 Set、映射 Map</strong>，所有的集合都扩展自 Iterable 特质 </p>
</li>
<li><p>于几乎所有的集合类，Scala 都同时提供了<strong>可变</strong>和<strong>不可变</strong>的版本，分别位于以下两个包</p>
<ul>
<li>不可变集合：scala.collection.immutable</li>
<li>可变集合：scala.collection.mutable</li>
</ul>
</li>
<li><p>Scala 不可变集合，就是指该集合对象不可修改，<strong>每次修改就会返回一个新对象</strong>，而不会对原对象进行修改。类似于 java 中的 String 对象</p>
</li>
<li><p>可变集合，就是这个集合可以<strong>直接对原对象进行修改</strong>，而不会返回新的对象。类似于 java 中 StringBuilder 对象</p>
</li>
</ol>
<p><code>建议：在操作集合的时候，不可变集合使用符号，可变集合使用方法</code></p>
<p><img src="/oct25-xxxxx/img/scala/13_%E4%B8%8D%E5%8F%AF%E5%8F%98%E9%9B%86%E5%90%88%E7%BB%A7%E6%89%BF%E5%9B%BE.png"></p>
<ol>
<li>Set、Map 是 Java 中也有的集合</li>
<li>Seq 是 Java 没有的，我们发现 List 归属到 Seq 了，因此这里的 List 就和 Java 不是同一个概念了</li>
<li>我们前面的 for 循环有一个 1 to 3，就是 IndexedSeq 下的 Range</li>
<li>String 也是属于 IndexedSeq</li>
<li>我们发现经典的数据结构比如 Queue 和 Stack 被归属到 LinearSeq(线性序列)</li>
<li>大家注意 Scala 中的 Map 体系有一个 SortedMap，说明 Scala 的 Map 可以支持排序</li>
<li>IndexedSeq 和 LinearSeq 的区别：<ul>
<li><strong>IndexedSeq</strong> 是通过索引来查找和定位，因此速度快，比如 String 就是一个索引集合，通过索引即可定位</li>
<li><strong>LinearSeq</strong> 是线型的，即有头尾的概念，这种数据结构一般是通过遍历来查找</li>
</ul>
</li>
</ol>
<p><img src="/oct25-xxxxx/img/scala/14_%E5%8F%AF%E5%8F%98%E9%9B%86%E5%90%88%E7%BB%A7%E6%89%BF%E5%9B%BE.png"></p>
<h1 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h1><h2 id="一、不可变数组-Array"><a href="#一、不可变数组-Array" class="headerlink" title="一、不可变数组 Array"></a>一、不可变数组 Array</h2><p>不可变数组有两种创建方式：</p>
<ul>
<li>第一种，通过 new 关键字创建，<code>val arr1 = new Array[Int](10)</code><ul>
<li>[Int] 表示存储数据的类型，如果希望存储任意类型数据，则可指定为 Any</li>
<li>(10) 表示数组大小，确定后不可变化</li>
<li>创建后，Int 类型数组里面的元素默认为 0</li>
</ul>
</li>
<li>第二种，通过调用伴生对象的 apply 方法，<code>val arr2 = Array(1, 2)</code><ul>
<li>在定义数组时，就可以直接赋初始值</li>
</ul>
</li>
</ul>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable.<span class="type">ArrayBuffer</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala可变数组ArrayBuffer</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala02_ArrayBuffer</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">// 一、定义可变数组</span></span><br><span class="line">    <span class="comment">//默认长度16</span></span><br><span class="line">    <span class="keyword">val</span> arr1 = <span class="keyword">new</span> <span class="type">ArrayBuffer</span>[<span class="type">Int</span>]()</span><br><span class="line">    <span class="keyword">val</span> arr2 = <span class="type">ArrayBuffer</span>(<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line">    println(arr1.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">// 二、添加元素</span></span><br><span class="line">    arr2.append(<span class="number">333</span>)</span><br><span class="line">    arr2.append(<span class="number">121</span>)</span><br><span class="line">    arr2.append(<span class="number">333</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    <span class="comment">// 在数组前面添加元素</span></span><br><span class="line">    arr2.prepend(<span class="number">1</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    <span class="comment">// 在指定位置添加</span></span><br><span class="line">    arr2.insert(<span class="number">2</span>, <span class="number">999</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在指定位置添加数组</span></span><br><span class="line">    arr2.insertAll(<span class="number">2</span>, arr1)</span><br><span class="line">    <span class="comment">// 在数组最后添加数组</span></span><br><span class="line">    arr2.appendAll(arr1)</span><br><span class="line">    <span class="comment">// 在数组最前添加数组</span></span><br><span class="line">    arr2.prependAll(arr1)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">// 三、修改元素</span></span><br><span class="line">    arr2.update(<span class="number">0</span>, <span class="number">888</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">// 四、删除元素</span></span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    arr2.remove(<span class="number">1</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    <span class="comment">// 从下标2开始，连续删除2个元素</span></span><br><span class="line">    arr2.remove(<span class="number">2</span>, <span class="number">2</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    arr2 -= <span class="number">333</span></span><br><span class="line">    println(arr2)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、可变数组-ArrayBuffer"><a href="#二、可变数组-ArrayBuffer" class="headerlink" title="二、可变数组 ArrayBuffer"></a>二、可变数组 ArrayBuffer</h2><p>定义可变数组也有两种创建方式：</p>
<ul>
<li>第一种，通过 new 关键字创建，<code>val arr1 = new ArrayBuffer[Int]()</code><ul>
<li>不指定长度的话，默认长度为16</li>
</ul>
</li>
<li>第二种，通过调用伴生对象的 apply 方法，<code>val arr2 = ArrayBuffer(1, 2)</code><ul>
<li>在定义数组时，就可以直接赋初始值</li>
</ul>
</li>
</ul>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable.<span class="type">ArrayBuffer</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala可变数组ArrayBuffer</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala02_ArrayBuffer</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">// 一、定义可变数组</span></span><br><span class="line">    <span class="comment">//默认长度16</span></span><br><span class="line">    <span class="keyword">val</span> arr1 = <span class="keyword">new</span> <span class="type">ArrayBuffer</span>[<span class="type">Int</span>]()</span><br><span class="line">    <span class="keyword">val</span> arr2 = <span class="type">ArrayBuffer</span>(<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line">    println(arr1.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">// 二、添加元素</span></span><br><span class="line">    arr2.append(<span class="number">333</span>)</span><br><span class="line">    arr2.append(<span class="number">121</span>)</span><br><span class="line">    arr2.append(<span class="number">333</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    <span class="comment">// 在数组前面添加元素</span></span><br><span class="line">    arr2.prepend(<span class="number">1</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    <span class="comment">// 在指定位置添加</span></span><br><span class="line">    arr2.insert(<span class="number">2</span>, <span class="number">999</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在指定位置添加数组</span></span><br><span class="line">    arr2.insertAll(<span class="number">2</span>, arr1)</span><br><span class="line">    <span class="comment">// 在数组最后添加数组</span></span><br><span class="line">    arr2.appendAll(arr1)</span><br><span class="line">    <span class="comment">// 在数组最前添加数组</span></span><br><span class="line">    arr2.prependAll(arr1)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">// 三、修改元素</span></span><br><span class="line">    arr2.update(<span class="number">0</span>, <span class="number">888</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">// 四、删除元素</span></span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    arr2.remove(<span class="number">1</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    <span class="comment">// 从下标2开始，连续删除2个元素</span></span><br><span class="line">    arr2.remove(<span class="number">2</span>, <span class="number">2</span>)</span><br><span class="line">    println(arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line">    arr2 -= <span class="number">333</span></span><br><span class="line">    println(arr2)</span><br><span class="line">    </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="三、可变数组与不可变数组转换"><a href="#三、可变数组与不可变数组转换" class="headerlink" title="三、可变数组与不可变数组转换"></a>三、可变数组与不可变数组转换</h2><p>通过两个方法实现互转：</p>
<ul>
<li>array.toBuffer：不可变 -&gt; 可变</li>
<li>arrayBuffer.toArray：可变 -&gt; 不可变</li>
</ul>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable</span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable.<span class="type">ArrayBuffer</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala可变数组与不可变数组转换</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala03_Convert</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> array = <span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>)</span><br><span class="line">    <span class="keyword">val</span> arrayBuffer = <span class="type">ArrayBuffer</span>(<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//将不可变数组转换为可变数组</span></span><br><span class="line">    <span class="keyword">val</span> buffer: mutable.<span class="type">Buffer</span>[<span class="type">Int</span>] = array.toBuffer</span><br><span class="line">    <span class="comment">//将可变数组转换为不可变数据</span></span><br><span class="line">    <span class="keyword">val</span> arr: <span class="type">Array</span>[<span class="type">Int</span>] = arrayBuffer.toArray</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>注：可变数组和不可变数组不会改变原有数组，只会返回一个新数组是可变或不可变的；</strong></p>
<h2 id="四、数组的遍历"><a href="#四、数组的遍历" class="headerlink" title="四、数组的遍历"></a>四、数组的遍历</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// todo 循环</span></span><br><span class="line"><span class="comment">//        array.foreach((i:Int) =&gt; &#123;println(i)&#125;)</span></span><br><span class="line"><span class="comment">//        array.foreach(println _ )</span></span><br><span class="line">        array.foreach(println)</span><br></pre></td></tr></table></figure>

<h2 id="五、多维数组"><a href="#五、多维数组" class="headerlink" title="五、多维数组"></a>五、多维数组</h2><p><strong>定义多维数组</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala多维数组</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala04_MultiArray</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建二维数组</span></span><br><span class="line">    <span class="keyword">val</span> array: <span class="type">Array</span>[<span class="type">Array</span>[<span class="type">Int</span>]] = <span class="type">Array</span>.ofDim(<span class="number">2</span>, <span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//访问数组</span></span><br><span class="line">    array(<span class="number">0</span>)(<span class="number">1</span>) = <span class="number">1</span></span><br><span class="line">    array(<span class="number">1</span>)(<span class="number">2</span>) = <span class="number">89</span></span><br><span class="line">    <span class="keyword">for</span> (i &lt;- array.indices; j &lt;- array(i).indices) &#123;</span><br><span class="line">      print(array(i)(j) + <span class="string">&quot;\t&quot;</span>)</span><br><span class="line">      <span class="keyword">if</span> (j == array(i).length - <span class="number">1</span>) println()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Seq-集合（List）"><a href="#Seq-集合（List）" class="headerlink" title="Seq 集合（List）"></a>Seq 集合（List）</h1><h2 id="一、不可变-List"><a href="#一、不可变-List" class="headerlink" title="一、不可变 List"></a>一、不可变 List</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala List集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala05_List</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">//List默认为不可变集合</span></span><br><span class="line">    <span class="comment">//创建一个List集合（数据有顺序，可重复）</span></span><br><span class="line">    <span class="keyword">val</span> list: <span class="type">List</span>[<span class="type">Int</span>] = <span class="type">List</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">5</span>)</span><br><span class="line">    println(list)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//遍历List</span></span><br><span class="line"><span class="comment">//    list.foreach(println)</span></span><br><span class="line">    <span class="comment">//取指定数据</span></span><br><span class="line"><span class="comment">//    println(list(2))</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//List增加元素</span></span><br><span class="line">    <span class="keyword">val</span> list2: <span class="type">List</span>[<span class="type">Int</span>] = list :+ <span class="number">333</span></span><br><span class="line">    <span class="keyword">val</span> list3: <span class="type">List</span>[<span class="type">Int</span>] = <span class="number">555</span> +: list</span><br><span class="line">    println(list2)</span><br><span class="line">    println(list3)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- --- &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//集合间合并，将一个整体拆成一个个的个体，称为扁平化</span></span><br><span class="line">    <span class="keyword">val</span> list4 = <span class="type">List</span>(<span class="number">8</span>, <span class="number">9</span>, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">val</span> list5: <span class="type">List</span>[<span class="type">Any</span>] = list4 :: <span class="number">88</span> :: list</span><br><span class="line">    println(list5) <span class="comment">//List(List(8, 9, 0), 88, 1, 2, 3, 4, 5, 5)</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> list6: <span class="type">List</span>[<span class="type">Int</span>] = list4 ::: <span class="number">66</span> :: list</span><br><span class="line">    println(list6) <span class="comment">//List(8, 9, 0, 66, 1, 2, 3, 4, 5, 5)</span></span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- --- &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//空集合Nil</span></span><br><span class="line">    <span class="keyword">val</span> list7: <span class="type">List</span>[<span class="type">Int</span>] = list ::: <span class="number">88</span> :: <span class="number">66</span> :: <span class="number">1</span> :: <span class="number">2</span> :: <span class="type">Nil</span></span><br><span class="line">    println(list7)</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、可变集合-ListBuffer"><a href="#二、可变集合-ListBuffer" class="headerlink" title="二、可变集合 ListBuffer"></a>二、可变集合 ListBuffer</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable.<span class="type">ListBuffer</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala 不可变集合ListBuffer</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala06_ListBuffer</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//可变集合ListBuffer</span></span><br><span class="line">    <span class="keyword">val</span> listBuffer: <span class="type">ListBuffer</span>[<span class="type">Int</span>] = <span class="keyword">new</span> <span class="type">ListBuffer</span>[<span class="type">Int</span>]</span><br><span class="line">    <span class="comment">//添加</span></span><br><span class="line">    listBuffer.append(<span class="number">1</span>)</span><br><span class="line">    listBuffer.appendAll(<span class="type">List</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>))    </span><br><span class="line">    listBuffer.prepend(<span class="number">88</span>)</span><br><span class="line">    println(listBuffer)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//修改</span></span><br><span class="line">    listBuffer.update(<span class="number">2</span>, <span class="number">88</span>)</span><br><span class="line">    listBuffer(<span class="number">0</span>) = <span class="number">99</span></span><br><span class="line">    println(listBuffer)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//删除</span></span><br><span class="line"><span class="comment">//    listBuffer.remove(1)</span></span><br><span class="line">    listBuffer.remove(<span class="number">1</span>, <span class="number">3</span>)</span><br><span class="line">    println(listBuffer)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//查询</span></span><br><span class="line">    listBuffer.foreach(println)    </span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> list1: <span class="type">ListBuffer</span>[<span class="type">Int</span>] = <span class="type">ListBuffer</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line">    <span class="keyword">val</span> list2: <span class="type">ListBuffer</span>[<span class="type">Int</span>] = <span class="type">ListBuffer</span>(<span class="number">2</span>, <span class="number">3</span>, <span class="number">666</span>, <span class="number">888</span>)</span><br><span class="line">    println(list1)</span><br><span class="line">    println(list2)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- --- &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> list3: <span class="type">ListBuffer</span>[<span class="type">Int</span>] = list1 ++ list2</span><br><span class="line">    println(list1)</span><br><span class="line">    println(list2)</span><br><span class="line">    println(list3)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- --- &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 注意：</span></span><br><span class="line"><span class="comment">     * 使用 ++ 不会改变原有集合，返回一个新的集合</span></span><br><span class="line"><span class="comment">     * 使用 ++= 会将结果保存到左侧集合，改变原有集合，同时也会返回一个新的集合</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">val</span> list4: list1.<span class="keyword">type</span> = list1 ++= list2</span><br><span class="line">    println(list1)</span><br><span class="line">    println(list2)</span><br><span class="line">    println(list4)</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Set-集合"><a href="#Set-集合" class="headerlink" title="Set 集合"></a>Set 集合</h1><h2 id="一、不可变-Set"><a href="#一、不可变-Set" class="headerlink" title="一、不可变 Set"></a>一、不可变 Set</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala Set集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala07_Set</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//不可变Set集合</span></span><br><span class="line">    <span class="keyword">val</span> set: <span class="type">Set</span>[<span class="type">Int</span>] = <span class="type">Set</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">7</span>, <span class="number">8</span>)</span><br><span class="line">    println(set)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 添加元素</span></span><br><span class="line"><span class="comment">     * Set无序，所以只有集合添加元素并没有添加前面还是后面的方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">val</span> set1: <span class="type">Set</span>[<span class="type">Int</span>] = set + <span class="number">12</span></span><br><span class="line">    println(set1)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 合并set</span></span><br><span class="line"><span class="comment">     * Set会去重，所以两个Set合并后，会去重并打乱重组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">val</span> set2: <span class="type">Set</span>[<span class="type">Int</span>] = <span class="type">Set</span>(<span class="number">1</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">66</span>, <span class="number">88</span>, <span class="number">75</span>)</span><br><span class="line">    <span class="keyword">val</span> set3: <span class="type">Set</span>[<span class="type">Int</span>] = set ++ set2</span><br><span class="line">    println(set3)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//删除元素</span></span><br><span class="line">    <span class="keyword">val</span> set4: <span class="type">Set</span>[<span class="type">Int</span>] = set3 - <span class="number">88</span></span><br><span class="line">    println(set4)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、可变-mutable-Set"><a href="#二、可变-mutable-Set" class="headerlink" title="二、可变 mutable.Set"></a>二、可变 mutable.Set</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala可变集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala08_MutableSet</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> set: mutable.<span class="type">Set</span>[<span class="type">Int</span>] = mutable.<span class="type">Set</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">3</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//添加元素</span></span><br><span class="line">    set.add(<span class="number">100</span>)</span><br><span class="line">    set += <span class="number">666</span></span><br><span class="line">    println(set)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//删除元素</span></span><br><span class="line">    set.remove(<span class="number">100</span>)</span><br><span class="line">    set -= <span class="number">2</span></span><br><span class="line">    println(set)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//查询</span></span><br><span class="line">    set.foreach(println)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 注：update方法并不是修改</span></span><br><span class="line"><span class="comment">     * 参数一：表示元素</span></span><br><span class="line"><span class="comment">     * 参数二：布尔值，true添加参数一到集合，false从集合中删除参数一</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    set.update(<span class="number">111111</span>, <span class="literal">true</span>)</span><br><span class="line">    println(set)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//集合合并</span></span><br><span class="line">    <span class="keyword">val</span> set2: mutable.<span class="type">Set</span>[<span class="type">Int</span>] = mutable.<span class="type">Set</span>(<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>)</span><br><span class="line">    <span class="keyword">val</span> set3: mutable.<span class="type">Set</span>[<span class="type">Int</span>] = set ++ set2</span><br><span class="line">    println(set3)    </span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 说明：</span></span><br><span class="line"><span class="comment">     * 使用 ++ 合并两个集合，原集合不变</span></span><br><span class="line"><span class="comment">     * 使用 ++= 合并的话，会将结果保存到左侧集合中，会改变原有集合</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    set ++= set2</span><br><span class="line">    println(set)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Map-集合"><a href="#Map-集合" class="headerlink" title="Map 集合"></a>Map 集合</h1><h2 id="一、不可变-Map"><a href="#一、不可变-Map" class="headerlink" title="一、不可变 Map"></a>一、不可变 Map</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala 不可变Map集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala09_Map</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建</span></span><br><span class="line">    <span class="keyword">val</span> map: <span class="type">Map</span>[<span class="type">String</span>, <span class="type">Int</span>] = <span class="type">Map</span>(<span class="string">&quot;a&quot;</span> -&gt; <span class="number">1</span>, <span class="string">&quot;b&quot;</span> -&gt; <span class="number">2</span>, <span class="string">&quot;c&quot;</span> -&gt; <span class="number">3</span>)</span><br><span class="line">    println(map)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//遍历</span></span><br><span class="line">    map.foreach(println)</span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- --- &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * map.keys：获取 Map 中所有的 key</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">for</span> (key &lt;- map.keys) &#123;</span><br><span class="line">      println(<span class="string">s&quot;集合 <span class="subst">$key</span> 对应值: <span class="subst">$&#123;map.get(key)&#125;</span>&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 注意：</span></span><br><span class="line"><span class="comment">     * map.get(key) 所得到的是一个 Some()，可以直接再调用 get方法获取具体的值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">val</span> option1: <span class="type">Option</span>[<span class="type">Int</span>] = map.get(<span class="string">&quot;a&quot;</span>)</span><br><span class="line">    println(option1.get)</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 补充一下</span></span><br><span class="line"><span class="comment">     * map.get(&quot;&quot;).get 等价于下面写法</span></span><br><span class="line"><span class="comment">     * 同时结果也一样，有值返回值，没值异常</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    println(map(<span class="string">&quot;a&quot;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * map中没有key=0的数据，所以获取不到，这里返回的就是 None</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">val</span> option2: <span class="type">Option</span>[<span class="type">Int</span>] = map.get(<span class="string">&quot;0&quot;</span>)</span><br><span class="line">    println(option2)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 可以使用下面方法避免空指定异常</span></span><br><span class="line"><span class="comment">     * 这里就表示，获取不到时，给个默认值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">val</span> res: <span class="type">Int</span> = map.getOrElse(<span class="string">&quot;akf8&quot;</span>, <span class="number">0</span>)</span><br><span class="line">    println(res)</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、可变-mutable-Map"><a href="#二、可变-mutable-Map" class="headerlink" title="二、可变 mutable.Map"></a>二、可变 mutable.Map</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala可变Map</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala10_MutableMap</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建</span></span><br><span class="line">    <span class="keyword">val</span> map: mutable.<span class="type">Map</span>[<span class="type">String</span>, <span class="type">Int</span>] = mutable.<span class="type">Map</span>(<span class="string">&quot;a&quot;</span> -&gt; <span class="number">1</span>, <span class="string">&quot;b&quot;</span> -&gt; <span class="number">2</span>, <span class="string">&quot;c&quot;</span> -&gt; <span class="number">3</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//添加数据</span></span><br><span class="line">    map += <span class="string">&quot;d&quot;</span> -&gt; <span class="number">4</span></span><br><span class="line">    println(map)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//添加值，并将原值返回</span></span><br><span class="line">    <span class="keyword">val</span> maybeInt: <span class="type">Option</span>[<span class="type">Int</span>] = map.put(<span class="string">&quot;a&quot;</span>, <span class="number">5</span>)</span><br><span class="line">    println(maybeInt.getOrElse(<span class="number">0</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">//删除数据</span></span><br><span class="line">    map -= <span class="string">&quot;b&quot;</span></span><br><span class="line">    println(map)</span><br><span class="line"><span class="comment">//    map -= (&quot;c&quot;, &quot;d&quot;)</span></span><br><span class="line"><span class="comment">//    println(map)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//修改数据</span></span><br><span class="line">    map.update(<span class="string">&quot;d&quot;</span>, <span class="number">88</span>)</span><br><span class="line">    map(<span class="string">&quot;a&quot;</span>) = <span class="number">666</span></span><br><span class="line">    println(map)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//遍历</span></span><br><span class="line">    map.foreach(println)</span><br><span class="line"></span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//合并集合</span></span><br><span class="line">    <span class="comment">//注意map2是个不可变map</span></span><br><span class="line">    <span class="keyword">val</span> map2: <span class="type">Map</span>[<span class="type">String</span>, <span class="type">Int</span>] = <span class="type">Map</span>(<span class="string">&quot;a&quot;</span> -&gt; <span class="number">1</span>, <span class="string">&quot;b&quot;</span> -&gt; <span class="number">2</span>, <span class="string">&quot;c&quot;</span> -&gt; <span class="number">3</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 将map2添加到map中</span></span><br><span class="line"><span class="comment">     * 注意：返过来是不行的，map2 ++= map 是不可以的，map2 是不可变的</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    map ++= map2</span><br><span class="line">    println(map)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * map2 ++= map 是不可以的</span></span><br><span class="line"><span class="comment">     * 但是 map2 ++ map 是可以的，这样需要返回一个新的map</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">val</span> map3: <span class="type">Map</span>[<span class="type">String</span>, <span class="type">Int</span>] = map2 ++ map</span><br><span class="line">    println(map3)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="元组Tuple"><a href="#元组Tuple" class="headerlink" title="元组Tuple"></a>元组Tuple</h1><p>元组也可以理解为一个容器，可以存放各种相同或不同类型的数据。说简单点，就是将多个无关的数据封装为一个整体，称为元组。</p>
<p><code>注意：元组中最大只能有22个元素</code></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala元组</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala11_tuple</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//声明元组</span></span><br><span class="line">    <span class="keyword">val</span> tuple: (<span class="type">Int</span>, <span class="type">String</span>, <span class="type">Boolean</span>) = (<span class="number">40</span>, <span class="string">&quot;zhangsan&quot;</span>, <span class="literal">true</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//访问元组</span></span><br><span class="line">    <span class="comment">//通过元素的顺序进行访问</span></span><br><span class="line">    println(tuple._1)</span><br><span class="line">    println(tuple._2)</span><br><span class="line">    println(tuple._3)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//通过索引访问</span></span><br><span class="line">    println(tuple.productElement(<span class="number">2</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//通过迭代器访问</span></span><br><span class="line">    <span class="keyword">for</span> (elem &lt;- tuple.productIterator) &#123;</span><br><span class="line">      println(elem)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * map中，键值对其实就是元组，只不过无组的元素个数为2，称之为对偶</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">val</span> map1: <span class="type">Map</span>[<span class="type">String</span>, <span class="type">Int</span>] = <span class="type">Map</span>(<span class="string">&quot;a&quot;</span> -&gt; <span class="number">1</span>, <span class="string">&quot;b&quot;</span> -&gt; <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">val</span> map2: <span class="type">Map</span>[<span class="type">String</span>, <span class="type">Int</span>] = <span class="type">Map</span>((<span class="string">&quot;a&quot;</span>, <span class="number">1</span>), (<span class="string">&quot;b&quot;</span>, <span class="number">2</span>))</span><br><span class="line"></span><br><span class="line">    map1.foreach(t =&gt; println(t._1 + <span class="string">&quot; &quot;</span> + t._2))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="集合常用函数"><a href="#集合常用函数" class="headerlink" title="集合常用函数"></a>集合常用函数</h1><h2 id="一、Normal正常"><a href="#一、Normal正常" class="headerlink" title="一、Normal正常"></a>一、Normal正常</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala集合常用方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala12_NormalFunction</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> list: <span class="type">List</span>[<span class="type">Int</span>] = <span class="type">List</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line">    <span class="comment">//head 表示第一个元素</span></span><br><span class="line">    println(list.head)</span><br><span class="line">    <span class="comment">//tail 尾部，这里表示除head之外，其他元素都是tail</span></span><br><span class="line">    <span class="comment">//返回的是List命令：List(2, 3, 4, 5)</span></span><br><span class="line">    println(list.tail)</span><br><span class="line">    <span class="comment">//last 表示最后一个元素</span></span><br><span class="line">    println(list.last)</span><br><span class="line">    <span class="comment">//init 初始，这里表示除了last之外，其他元素都是init</span></span><br><span class="line">    <span class="comment">//返回的是List命令：List(1, 2, 3, 4)</span></span><br><span class="line">    println(list.init)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//求和</span></span><br><span class="line">    println(list.sum)</span><br><span class="line">    <span class="comment">//最大值</span></span><br><span class="line">    println(list.max)</span><br><span class="line">    <span class="comment">//最小值</span></span><br><span class="line">    println(list.min)</span><br><span class="line">    <span class="comment">//乘积</span></span><br><span class="line">    println(list.product)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、Medium一般"><a href="#二、Medium一般" class="headerlink" title="二、Medium一般"></a>二、Medium一般</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala集合常用方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala13_MediumFunction</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> list1: <span class="type">List</span>[<span class="type">Int</span>] = <span class="type">List</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line">    <span class="keyword">val</span> list2: <span class="type">List</span>[<span class="type">Int</span>] = <span class="type">List</span>(<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> numList: <span class="type">List</span>[<span class="type">List</span>[<span class="type">Int</span>]] = <span class="type">List</span>(<span class="type">List</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>), <span class="type">List</span>(<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>))</span><br><span class="line">    <span class="keyword">val</span> wordList: <span class="type">List</span>[<span class="type">String</span>] = <span class="type">List</span>(<span class="string">&quot;hello world&quot;</span>, <span class="string">&quot;hadoop hadoop&quot;</span>, <span class="string">&quot;scala kafka spark&quot;</span>, <span class="string">&quot;hello scala&quot;</span>, <span class="string">&quot;hello&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//todo 1、生成字符串，使用指定分隔符</span></span><br><span class="line">    println(list1.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 2、是否包含</span></span><br><span class="line">    println(list1.contains(<span class="number">222</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 3、循环遍历，直接打印</span></span><br><span class="line">    list1.foreach(println)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 4、从集合中取N条数据，返回集合</span></span><br><span class="line">    println(list1.take(<span class="number">3</span>))</span><br><span class="line">    println(list1.takeRight(<span class="number">1</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 5、集合数据的反转</span></span><br><span class="line">    println(list1.reverse)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 6、映射转换，A =&gt; B</span></span><br><span class="line">    <span class="comment">//将每个元素乘5</span></span><br><span class="line"><span class="comment">//    list1.map(n =&gt; &#123; n * 5 &#125;)</span></span><br><span class="line">    println(list1.map(_ * <span class="number">5</span>))</span><br><span class="line">    <span class="comment">//将每个元素转换为字符串输出</span></span><br><span class="line"><span class="comment">//    list1.foreach(n =&gt; &#123; n.toString &#125;)</span></span><br><span class="line">    println(list1.map(_.toString))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 7、扁平化</span></span><br><span class="line">    println(numList.flatten)</span><br><span class="line">    <span class="comment">//扁平化 + 映射</span></span><br><span class="line">    println(numList.flatten.map( _ * <span class="number">2</span> ))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 8、扁平映射</span></span><br><span class="line"><span class="comment">//    list1.flatMap(num =&gt; List(num))</span></span><br><span class="line">    println(list1.flatMap(<span class="type">List</span>(_)))</span><br><span class="line"><span class="comment">//    println(numList.flatMap(list1 =&gt; list1.map(_ * 2)))</span></span><br><span class="line">    println(numList.flatMap(list =&gt; list.map(_ * <span class="number">2</span>)))</span><br><span class="line">    println(wordList.flatMap(_.split(<span class="string">&quot; &quot;</span>)))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 9、过滤，true保留，false丢弃</span></span><br><span class="line">    <span class="comment">//保留偶数</span></span><br><span class="line">    println(list1.filter(_ % <span class="number">2</span> == <span class="number">0</span>))</span><br><span class="line">    <span class="comment">//将单词中带s的保留</span></span><br><span class="line">    println(wordList.flatMap(_.split(<span class="string">&quot; &quot;</span>))</span><br><span class="line">      .map( _.toLowerCase)</span><br><span class="line">      .filter(_.contains(<span class="string">&quot;s&quot;</span>)))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 10、分组</span></span><br><span class="line">    <span class="comment">//1》偶数一组，奇数一组</span></span><br><span class="line">    <span class="comment">//返回map，key为分组key，value为组的集合</span></span><br><span class="line">    println(list1.groupBy(_ % <span class="number">2</span>))</span><br><span class="line">    <span class="comment">//2》首字母相同放一组</span></span><br><span class="line">    println(wordList.flatMap(_.split(<span class="string">&quot; &quot;</span>))</span><br><span class="line">      .groupBy(_.charAt(<span class="number">0</span>)))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 11、排序</span></span><br><span class="line">    <span class="comment">//默认升序</span></span><br><span class="line">    println(list1.sortBy(n =&gt; n))</span><br><span class="line">    <span class="comment">//通过反转降序</span></span><br><span class="line">    println(list1.sortBy(n =&gt; n).reverse)</span><br><span class="line">    <span class="comment">//降序</span></span><br><span class="line">    println(list1.sortBy(n =&gt; n)(<span class="type">Ordering</span>.<span class="type">Int</span>.reverse))</span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- ---&quot;</span>)</span><br><span class="line">    <span class="comment">//降序</span></span><br><span class="line">    println(list1.sortWith((l, r) =&gt; l &lt; r))</span><br><span class="line">    <span class="comment">//升序</span></span><br><span class="line">    println(list1.sortWith((l, r) =&gt; l &gt; r))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 12、wordCount练习</span></span><br><span class="line">    <span class="keyword">val</span> wordCount: <span class="type">List</span>[(<span class="type">String</span>, <span class="type">Int</span>)] = wordList</span><br><span class="line">      .flatMap(_.split(<span class="string">&quot; &quot;</span>))</span><br><span class="line">      .map((_, <span class="number">1</span>))</span><br><span class="line">      .groupBy(_._1)</span><br><span class="line">      .map(t =&gt; (t._1, t._2.size))</span><br><span class="line">      .toList</span><br><span class="line">      .sortWith((lift, right) =&gt; lift._2 &gt; right._2)</span><br><span class="line">      .take(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 13、迭代器</span></span><br><span class="line">    <span class="keyword">val</span> iterator: <span class="type">Iterator</span>[<span class="type">Int</span>] = list1.iterator</span><br><span class="line">    <span class="keyword">while</span> (iterator.hasNext) &#123;</span><br><span class="line">      println(iterator.next())</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//todo 14、是否包含</span></span><br><span class="line">    println(list1.contains(<span class="number">5</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line">    <span class="comment">//交集</span></span><br><span class="line">    println(list1.intersect(list2))</span><br><span class="line">    <span class="comment">//并集</span></span><br><span class="line">    println(list1.concat(list2))</span><br><span class="line">    <span class="comment">//差集</span></span><br><span class="line">    println(list1.diff(list2))</span><br><span class="line">    <span class="comment">//拉链，List((1,3), (2,4), (3,5), (4,6), (5,7))</span></span><br><span class="line">    println(list1.zip(list2))</span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- ---&quot;</span>)</span><br><span class="line">    <span class="comment">//滑窗</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    List(1, 2)</span></span><br><span class="line"><span class="comment">    List(4, 5)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    list1.sliding(<span class="number">2</span>, <span class="number">3</span>).foreach(println)</span><br><span class="line">    </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="三、Medium-1一般"><a href="#三、Medium-1一般" class="headerlink" title="三、Medium-1一般"></a>三、Medium-1一般</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala集合常用方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala14_MediumFunction1</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> list1: <span class="type">List</span>[<span class="type">Int</span>] = <span class="type">List</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line">    <span class="keyword">val</span> list2: <span class="type">List</span>[<span class="type">Int</span>] = <span class="type">List</span>(<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//交集</span></span><br><span class="line">    println(list1.intersect(list2))</span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- ---&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//并集</span></span><br><span class="line">    println(list1.concat(list2))</span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- ---&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//差集</span></span><br><span class="line">    println(list1.diff(list2))</span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- ---&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//拉链，List((1,3), (2,4), (3,5), (4,6), (5,7))</span></span><br><span class="line">    <span class="comment">//两个集合长度不一致时，多余的元素丢弃</span></span><br><span class="line">    println(list1.zip(list2))</span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- ---&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//滑窗</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    List(1, 2)</span></span><br><span class="line"><span class="comment">    List(4, 5)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    list1.sliding(<span class="number">2</span>, <span class="number">3</span>).foreach(println)</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、Hell地狱"><a href="#四、Hell地狱" class="headerlink" title="四、Hell地狱"></a>四、Hell地狱</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala集合常用方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala15_Hell</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> list: <span class="type">List</span>[<span class="type">Int</span>] = <span class="type">List</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//简化（规约）</span></span><br><span class="line">    println(list.reduce((x, y) =&gt; x + y))</span><br><span class="line">    println(list.reduce(_ + _))</span><br><span class="line"></span><br><span class="line">    <span class="comment">//reduce在底层就是调用 reduceLeft</span></span><br><span class="line">    println(list.reduceLeft(_ + _))</span><br><span class="line"></span><br><span class="line">    <span class="comment">//reduceRight底层就是先将集合反转，然后调用reduceLift，并且在计算时xy交换位置</span></span><br><span class="line">    <span class="comment">//加法不变</span></span><br><span class="line">    println(list.reduceRight(_ + _))</span><br><span class="line">    <span class="comment">//原集合：(1,2,3,4,5)</span></span><br><span class="line">    <span class="comment">//反转：(5,4,3,2,1)</span></span><br><span class="line">    <span class="comment">//调用reduceLeft，并将xy交换位置，(1 - (2 - (3 - (4 - 5)))</span></span><br><span class="line">    println(list.reduceRight(_ - _))</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、-Hell-1地狱"><a href="#五、-Hell-1地狱" class="headerlink" title="五、 Hell-1地狱"></a>五、 Hell-1地狱</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala集合常用方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala16_Hell1</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> list: <span class="type">List</span>[<span class="type">Int</span>] = <span class="type">List</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//折叠</span></span><br><span class="line"><span class="comment">//    println(list.fold(5)(_ + _))</span></span><br><span class="line"><span class="comment">//    println(list.foldLeft(5)(_ + _))</span></span><br><span class="line"><span class="comment">//    println(list.foldRight(5)(_ + _))</span></span><br><span class="line">    println(list.fold(<span class="number">5</span>)(_ - _))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 需求：将两个map合并，相同key的value进行累加</span></span><br><span class="line">    <span class="keyword">val</span> map1 = mutable.<span class="type">Map</span>((<span class="string">&quot;a&quot;</span>, <span class="number">1</span>), (<span class="string">&quot;b&quot;</span>, <span class="number">2</span>), (<span class="string">&quot;c&quot;</span>, <span class="number">3</span>))</span><br><span class="line">    <span class="keyword">val</span> map2 = mutable.<span class="type">Map</span>((<span class="string">&quot;a&quot;</span>, <span class="number">4</span>), (<span class="string">&quot;d&quot;</span>, <span class="number">5</span>), (<span class="string">&quot;c&quot;</span>, <span class="number">6</span>))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> result: mutable.<span class="type">Map</span>[<span class="type">String</span>, <span class="type">Int</span>] = map1.foldLeft(map2)((map, kv) =&gt; &#123;</span><br><span class="line">      <span class="keyword">val</span> k = kv._1</span><br><span class="line">      <span class="keyword">val</span> v = kv._2</span><br><span class="line">      map(k) = map.getOrElse(k, <span class="number">0</span>) + v</span><br><span class="line">      map</span><br><span class="line">    &#125;)</span><br><span class="line">    println(result)</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="队列"><a href="#队列" class="headerlink" title="队列"></a>队列</h1><p>Scala也提供了队列（Queue）的数据结构，队列的特点就是先进先出。进队和出队的方法分别为 enqueue 和 dequeue。</p>
<p>注意：不可变队列 immutable.Queue，在执行入队操作后，原队列是不会被插入数据的，但是 enqueue 方法会返回新队列。</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter09</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.collection.immutable.<span class="type">Queue</span></span><br><span class="line"><span class="keyword">import</span> scala.collection.mutable</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala队列Queue</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala17_Queue</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建一个队列</span></span><br><span class="line">    <span class="keyword">val</span> queue: mutable.<span class="type">Queue</span>[<span class="type">String</span>] = <span class="keyword">new</span> mutable.<span class="type">Queue</span></span><br><span class="line">    queue.enqueue(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>)</span><br><span class="line">    println(queue)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- --- &quot;</span>)</span><br><span class="line">    <span class="comment">//出队</span></span><br><span class="line">    println(queue.dequeue())</span><br><span class="line">    println(queue)</span><br><span class="line">    println(queue.dequeue())</span><br><span class="line">    println(queue)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;--- --- --- --- --- --- &quot;</span>)</span><br><span class="line">    <span class="comment">//入队</span></span><br><span class="line">    queue.enqueue(<span class="string">&quot;e&quot;</span>, <span class="string">&quot;f&quot;</span>)</span><br><span class="line">    println(queue)</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;=== === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建不可变集合</span></span><br><span class="line">    <span class="keyword">val</span> immutableQueue: <span class="type">Queue</span>[<span class="type">String</span>] = <span class="type">Queue</span>(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>)</span><br><span class="line">    <span class="comment">//入队后，原队列不变，但是会返回新队列</span></span><br><span class="line">    <span class="keyword">val</span> newQueue: <span class="type">Queue</span>[<span class="type">String</span>] = immutableQueue.enqueue(<span class="string">&quot;d&quot;</span>)</span><br><span class="line">    println(immutableQueue)</span><br><span class="line">    println(newQueue)</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Oct25-X</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/">https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://oct25-x.atomgit.net/oct25-xxxxx" target="_blank">Oct25-X</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/oct25-xxxxx/tags/Scala/">Scala</a></div><div class="post_share"><div class="social-share" data-image="/oct25-xxxxx/img/cover_img/ctt50.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/"><img class="prev-cover" src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">大数据之多范式编程语言 Scala08_模式匹配</div></div></a></div><div class="next-post pull-right"><a href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/"><img class="next-cover" src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">大数据之多范式编程语言 Scala06_面向对象</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/oct25-xxxxx/2024/02/18/44%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala01_%E5%85%A5%E9%97%A8/" title="大数据之多范式编程语言 Scala01_入门"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt44.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-18</div><div class="title">大数据之多范式编程语言 Scala01_入门</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/19/46%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala03_%E8%BF%90%E7%AE%97%E7%AC%A6/" title="大数据之多范式编程语言 Scala03_运算符"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt46.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-19</div><div class="title">大数据之多范式编程语言 Scala03_运算符</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/18/45%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala02_%E5%8F%98%E9%87%8F%E5%92%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" title="大数据之多范式编程语言 Scala02_变量和数据类型"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt45.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-18</div><div class="title">大数据之多范式编程语言 Scala02_变量和数据类型</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/20/47%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala04_%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/" title="大数据之多范式编程语言 Scala04_流程控制"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt47.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-20</div><div class="title">大数据之多范式编程语言 Scala04_流程控制</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt48.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-20</div><div class="title">大数据之多范式编程语言 Scala05_函数式编程</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt49.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-21</div><div class="title">大数据之多范式编程语言 Scala06_面向对象</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Oct25-X</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E7%BB%A7%E6%89%BF%E5%9B%BE"><span class="toc-number">1.</span> <span class="toc-text">集合继承图</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%95%B0%E7%BB%84"><span class="toc-number">2.</span> <span class="toc-text">数组</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E4%B8%8D%E5%8F%AF%E5%8F%98%E6%95%B0%E7%BB%84-Array"><span class="toc-number">2.1.</span> <span class="toc-text">一、不可变数组 Array</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8F%AF%E5%8F%98%E6%95%B0%E7%BB%84-ArrayBuffer"><span class="toc-number">2.2.</span> <span class="toc-text">二、可变数组 ArrayBuffer</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%8F%AF%E5%8F%98%E6%95%B0%E7%BB%84%E4%B8%8E%E4%B8%8D%E5%8F%AF%E5%8F%98%E6%95%B0%E7%BB%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.3.</span> <span class="toc-text">三、可变数组与不可变数组转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E6%95%B0%E7%BB%84%E7%9A%84%E9%81%8D%E5%8E%86"><span class="toc-number">2.4.</span> <span class="toc-text">四、数组的遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%A4%9A%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="toc-number">2.5.</span> <span class="toc-text">五、多维数组</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Seq-%E9%9B%86%E5%90%88%EF%BC%88List%EF%BC%89"><span class="toc-number">3.</span> <span class="toc-text">Seq 集合（List）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E4%B8%8D%E5%8F%AF%E5%8F%98-List"><span class="toc-number">3.1.</span> <span class="toc-text">一、不可变 List</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8F%AF%E5%8F%98%E9%9B%86%E5%90%88-ListBuffer"><span class="toc-number">3.2.</span> <span class="toc-text">二、可变集合 ListBuffer</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Set-%E9%9B%86%E5%90%88"><span class="toc-number">4.</span> <span class="toc-text">Set 集合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E4%B8%8D%E5%8F%AF%E5%8F%98-Set"><span class="toc-number">4.1.</span> <span class="toc-text">一、不可变 Set</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8F%AF%E5%8F%98-mutable-Set"><span class="toc-number">4.2.</span> <span class="toc-text">二、可变 mutable.Set</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Map-%E9%9B%86%E5%90%88"><span class="toc-number">5.</span> <span class="toc-text">Map 集合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E4%B8%8D%E5%8F%AF%E5%8F%98-Map"><span class="toc-number">5.1.</span> <span class="toc-text">一、不可变 Map</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8F%AF%E5%8F%98-mutable-Map"><span class="toc-number">5.2.</span> <span class="toc-text">二、可变 mutable.Map</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%83%E7%BB%84Tuple"><span class="toc-number">6.</span> <span class="toc-text">元组Tuple</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0"><span class="toc-number">7.</span> <span class="toc-text">集合常用函数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81Normal%E6%AD%A3%E5%B8%B8"><span class="toc-number">7.1.</span> <span class="toc-text">一、Normal正常</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81Medium%E4%B8%80%E8%88%AC"><span class="toc-number">7.2.</span> <span class="toc-text">二、Medium一般</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81Medium-1%E4%B8%80%E8%88%AC"><span class="toc-number">7.3.</span> <span class="toc-text">三、Medium-1一般</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81Hell%E5%9C%B0%E7%8B%B1"><span class="toc-number">7.4.</span> <span class="toc-text">四、Hell地狱</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81-Hell-1%E5%9C%B0%E7%8B%B1"><span class="toc-number">7.5.</span> <span class="toc-text">五、 Hell-1地狱</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%98%9F%E5%88%97"><span class="toc-number">8.</span> <span class="toc-text">队列</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"><img src="/oct25-xxxxx/img/cover_img/ctt52.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型">大数据之多范式编程语言 Scala09_隐式转换、异常和泛型</a><time datetime="2024-02-24T04:55:34.000Z" title="发表于 2024-02-24 12:55:34">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配"><img src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala08_模式匹配"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配">大数据之多范式编程语言 Scala08_模式匹配</a><time datetime="2024-02-24T00:46:12.000Z" title="发表于 2024-02-24 08:46:12">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img src="/oct25-xxxxx/img/cover_img/ctt50.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala07_集合"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合">大数据之多范式编程语言 Scala07_集合</a><time datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala06_面向对象"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象">大数据之多范式编程语言 Scala06_面向对象</a><time datetime="2024-02-21T07:01:10.000Z" title="发表于 2024-02-21 15:01:10">2024-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala05_函数式编程"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程">大数据之多范式编程语言 Scala05_函数式编程</a><time datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg24.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Oct25-X</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></a><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/oct25-xxxxx/js/utils.js"></script><script src="/oct25-xxxxx/js/main.js"></script><script src="/oct25-xxxxx/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/oct25-xxxxx/js/search/local-search.js"></script><div class="js-pjax"></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>